/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fragment;

import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.table.api.TableEnvironment;

public class IcebergDeletedSink implements SqlFragment {

    @Override
    public String createConnector(ParameterTool pt, TableEnvironment tableEnv) {
        String icebergHiveMeta = pt.get("ICEBERG_HIVE_META");
        String icebergDatabase = pt.get("ICEBERG_DATABASE");
        String icebergDeletedTablePattern = pt.get("ICEBERG_DELETED_TABLE_PATTERN", "${table}_delete");

        String sinkSql2 = String.format(
                "CREATE TABLE `sink_table_iceberg_deleted`(\n"
                        + "    `type` INT,\n"
                        + "    `phase` INT,\n"
                        + "    `data_source` STRING,\n"
                        + "    `namespace` STRING,\n"
                        + "    `schema` STRING,\n"
                        + "    `table` STRING,\n"
                        + "    `record` bytes,\n"
                        + "    `ddl` bytes,\n"
                        + "    `meta` STRING)\n"
                        + "    WITH (\n"
                        + "    'sink.multiple.enable' = 'true',\n"
                        + "    'connector' = 'iceberg-inlong-r4',\n"
                        + "    'schema.uid' = 'schema_uid_2',\n"
                        + "    'append-mode' = 'true',\n"
                        + "    'sink.schema-change.policies' = '1=1&2=2&3=2&4=2&5=2&6=2&7=2&8=2',\n"
                        + "    'switch.append.upsert.enable' = 'true',\n"
                        + "    'catalog-type' = 'HIVE',\n"
                        + "    'catalog-name' = 'HIVE',\n"
                        + "    'sink.multiple.table-fields-cases' = 'LOWERCASE',\n"

                        + "    'sink.multiple.database-pattern' = '%s',\n"
                        + "    'sink.multiple.table-pattern' = '%s',\n"
                        + "    'uri' = '%s'\n"
                        + ")", icebergDatabase, icebergDeletedTablePattern, icebergHiveMeta);
        LOGGER.info("deleted sink is \n {}", sinkSql2);
        tableEnv.executeSql(sinkSql2);

        return "sink_table_iceberg_deleted";
    }
}
